/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mahumLogs.utils;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import java.io.File;
import java.util.HashMap;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Kalast
 */
public class SSH {
    
    public static final int OS_WINDOWS = 0;
    public static final int OS_LINUX = 1;
    
    public static final String LIST_FILES = "LF";
    public static final String COUNT_LINES_FILE = "CLF";
    
    private static final HashMap<String, String[]> CMDS_MAP = new HashMap();
    
    static{
        CMDS_MAP.put("LF", new String[]{"cmd dir \"%\\$0$/%\" /b", "ls \"%\\$0$/%\""});
        CMDS_MAP.put("CLF", new String[]{"type \"%\\$0$/%\" | find /c /v \"\"", "wc -l \"%\\$0$/%\""});
    }
    
    public static int OS_SSH = OS_LINUX;
    
    public static String getValidPathFile(String path){
        if(SSH.OS_SSH == SSH.OS_LINUX){
            return path.replace("\\", "/");
        } else {
            return path.replace("/", "\\");
        }
    }
    
    public static String cmdOS(String cmd, String...params){
        String patternCmd = SSH.CMDS_MAP.get(cmd)[SSH.OS_SSH];
        for(int i = 0; i < params.length; i++){
            System.out.println(i + ":param = " + params[i]);
            patternCmd = patternCmd.replace("%\\$" + i + "$/%", params[i]);
        }
        
        return patternCmd;
    }
    
    /**
     * @param host
     * @param user
     * @param pwd
     * @return
     * @throws JSchException 
     */
    public static Session createSession(String host, String user, final String pwd) throws JSchException{
        JSch client = new JSch();
        Session session = client.getSession(user, host);
        
                System.out.println("Create Session " + host + "@" + user + ":" + pwd);
        
        UserInfo info = new UserInfo() {
                
                @Override
                public String getPassphrase() {
                    return "";
                }
                
                @Override
                public String getPassword() {
                    return pwd;
                }
                
                @Override
                public boolean promptPassword(String string) {
                    return true;
                }
                
                @Override
                public boolean promptPassphrase(String string) {
                    return true;
                }
                
                @Override
                public boolean promptYesNo(String string) {
                    return true;
                }
                
                @Override
                public void showMessage(String string) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };
        
        session.setUserInfo(info);
        session.setTimeout(30000);
        
        return session;
    }
}
